﻿using System;
using System.Linq;
using Hjt212Parser;
using log4net;
using TcpServer.Facade;
using TcpServer.Orm;

namespace TcpServer.DataProcessor
{
    class LatitudeLongitude2031Processor : IProcessor
    {
        private readonly ILog log = LogManager.GetLogger(typeof(LatitudeLongitude2031Processor));

        public void Persist()
        {
            throw new NotImplementedException();
        }

        public void Persist(DustEntities context, SocketClientInfo content)
        {
            if (string.IsNullOrWhiteSpace(content.MN)) return;
            try
            {
                //有效字符串
                if (Parser.IsLengthRight(content.ReceivedMsg))
                {
                    //根据MN号获取基本信息
                    var equipment = BaseInfoBufferAndHelper.GetBaseWholeInfoByMn(content.MN);
                    //有效企业信息
                    if (equipment != null)
                    {
                        var main =
                            context.SiteInfors.FirstOrDefault(t => t.ID == equipment.DevCodeID && t.SiteTypeID != null && t.SiteTypeID.Value == equipment.工地类型ID);
                        if (main != null)
                        {
                            var lat = Parser.GetDoubleByKey("a60001", content.ReceivedMsg);
                            var lon= Parser.GetDoubleByKey("a60002", content.ReceivedMsg);
                            if(lat==null||lat.Value<1|| lon == null || lon.Value < 1) return;
                            main.Latitude = Parser.GetDoubleByKey("a60001", content.ReceivedMsg);
                            main.Longitude = Parser.GetDoubleByKey("a60002", content.ReceivedMsg);
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        log.Warn(content.MN + " 没有对应基础信息！");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error( ex);
                log.Error( content.ReceivedMsg);
            }
        }
        public void Persist(DataCommunicationEntities context, SocketClientInfo content)
        {
            throw new NotImplementedException();
        }
    }
}
